//
//  SZHUDTool.swift
//  ShenZhouDaDiXing
//
//  Created by spectator Mr.Z on 2016/12/31.
//  Copyright © 2016年 ZZC WORKSPACE. All rights reserved.
//

import UIKit
import SVProgressHUD

class CLHUDTool: NSObject {
    
}

extension CLHUDTool {
    
    func defaultSet() {
        SVProgressHUD.setDefaultStyle(.dark)
        SVProgressHUD.setDefaultAnimationType(.flat)
        SVProgressHUD.setDefaultMaskType(.clear)
    }
    
    func touchDis() {
        SVProgressHUD.setDefaultStyle(.dark)
        SVProgressHUD.setDefaultAnimationType(.native)
        SVProgressHUD.setDefaultMaskType(.gradient)
    }
}

func CLHUDDismiss() {
    if SVProgressHUD.isVisible() {
        SVProgressHUD.dismiss()
    }
}

fileprivate func CLHUDLoading(_ message: String) {
    CLHUDTool().defaultSet()
    if message == "" {
        SVProgressHUD.show()
    }else{
        SVProgressHUD.show(withStatus: message)
    }
}

fileprivate func CLHUDInfo(_ message: String) {
    CLHUDTool().defaultSet()
    SVProgressHUD.showInfo(withStatus: message)
}

fileprivate func CLHUDSuccess(_ message: String) {
    CLHUDTool().defaultSet()
    SVProgressHUD.showSuccess(withStatus: message)
}

enum CLHUDType {
    case error,info,success,loading
}

func CLHUD(_ message: String = "", type: CLHUDType, time: TimeInterval = 1, callBack: (() -> ())?) {
    switch type {
    case .error:
        CLHUDError(message)
    case .info:
        CLHUDInfo(message)
    case .success:
        CLHUDSuccess(message)
    case .loading:
        CLHUDLoading(message)
    }
    //    if let timeL = time {
    //    if type != .loading {
    //        _ = delay(time, task: {
    //            SZHUDDismiss()
    //            callBack?()
    //        })
    
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time, execute: {
        //            SZHUDDismiss()
        CLHUDDismiss()
        callBack?()
    })
    //    }
    
    //    DispatchQueue.
    //    }
    
}

func CLHUD(_ image: UIImage, message: String) {
    CLHUDTool().defaultSet()
    SVProgressHUD.show(image, status: message)
}
fileprivate func CLHUDError(_ message: String) {
    CLHUDTool().defaultSet()
    SVProgressHUD.showError(withStatus: message)
}
